package com.baosight.sczx.common.zm.basetools.listener;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.dao.Dao;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.zm.cm.domain.ZMCMMaterialRation;

/**
 * 
 * <p>Title: GenerateMonthCharge.java</p>
 * <p>Description: 生成各月各生产单位费用额度</p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author wangkun
 * @version 1.0
 * <pre>Histroy:
 *          2012-2-22 下午04:07:57  wangkun  Create
 *</pre>
 */
public class GenerateMonthCharge {
	private Dao dao;
	
	public GenerateMonthCharge(){
	}
	
	public void setDao(Dao dao)
	{
		this.dao = dao;
	}
	
	/**
	 * 
	 * <p>Function: GenerateCharge</p>
	 * <p>Description: 根据上月数据生成下月费用额度</p>
	 * <p>return value:void</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-2-22 下午04:07:57  wangkun
	 * </p>
	 *
	 */
	public void GenerateCharge() throws Exception 
	{
		String currentYear = StaticTools.getYear();
		String currentMonth = StaticTools.getMonth();
		String currentDay = StaticTools.getDateforDay().substring(2);
		int n_currentYear = Integer.parseInt(currentYear);
		int n_currentMonth = Integer.parseInt(currentMonth);
		int n_nextYear = n_currentYear;
		int n_nextMonth = n_currentMonth;
		
		if(n_currentMonth == 12)
		{
			n_nextYear = n_currentYear + 1;
			n_nextMonth = 1;
		}
		else
		{
			n_nextYear = n_currentYear;
			n_nextMonth = n_currentMonth + 1;
		}
		
		//如果日期等于28号，才执行
		if(!"28".equals(currentDay))
		{
			return;
		}
		else
		{
			Map<String, String> _argMap = new HashMap<String, String>();
			_argMap.put("fdMonth", n_nextMonth+"");
			_argMap.put("fdYear", currentYear);
			List<?> _list = dao.query("ZMCMMaterialRation.query", _argMap);
			
			//如果下月记录已存在，不再执行
			if(_list.size() > 0)
			{
				return;
			}
			
			_argMap = new HashMap<String, String>();
			_argMap.put("fdMonth", n_currentMonth+"");
			_argMap.put("fdYear", currentYear);
			
			List<?> _result = dao.query("ZMCMMaterialRation.query", _argMap);
			
			for(int i=0; i<_result.size(); i++)
			{
				String keyId = TablePk.getInstance().getNextValue("T_CM_MATERIALRATION");
				
				ZMCMMaterialRation materialRation = (ZMCMMaterialRation)_result.get(i);
				
				Map<String, String> _insertMap = new HashMap<String, String>();
				_insertMap.put("fdKeyId", keyId);
				_insertMap.put("fdDepartmentId", materialRation.getFdDepartmentId());
				_insertMap.put("fdYear", n_nextYear+"");
				_insertMap.put("fdMonth", n_nextMonth+"");
				_insertMap.put("fdMonthRation", materialRation.getFdMonthRation()+"");
				
				dao.insert("ZMCMMaterialRation.insert", _insertMap);
			}
		}
	}
}
